package com.applink.sell_proof;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.api.DataResult;
import com.applink.sell_proof.api.Methods;
import com.applink.sell_proof.util.Utility;

public class ChangePassword extends Activity implements OnClickListener,Observer {
	
	private TextView btnChagePassword;
	private Context mContext;
	public static final String TAG = "ChangePassword";
	private ProgressDialog progressDialog;
	private String changePassword = "";
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.change_password);
		mContext = this;
		initView();
	}
	
	
	@Override
	public void onResume() {
		super.onResume();
		DataHelper.getInstance().addObserver(this);
	}
	
	@Override
	public void onPause() {
		super.onPause();
		DataHelper.getInstance().deleteObserver(this);
	}
	
	private void initView(){
		progressDialog = new ProgressDialog(mContext);
		if(btnChagePassword == null){
			btnChagePassword = (TextView) findViewById(R.id.btn_change_pass);
			btnChagePassword.setOnClickListener(this);
		}
	}

	@Override
	public void onClick(View v) {
		String regex = "^([A-Za-z]+)([0-9]+)|([0-9]+)([A-Za-z])$";
		String pass1 = ((TextView)findViewById(R.id.login_pass1)).getText().toString();
		String pass2 = ((TextView)findViewById(R.id.login_pass2)).getText().toString();
		if(pass1.trim().length() == 0||pass2.trim().length() == 0) {
			Utility.showAlertDialog(mContext, android.R.drawable.ic_dialog_alert, getString(R.string.error_enter_both_pass), "Change password");
			
			return;
		}
		if(pass1.equals(pass2)) {
			if(pass1.trim().length() == 0) {
				Utility.showAlertDialog(mContext, android.R.drawable.ic_dialog_alert, getString(R.string.error_enter_both_pass), "Change password");
				
			} else if(pass1.trim().length() < 4 ||pass1.trim().matches(regex) == false) {
				changePassword = pass1.toString().trim();
				Utility.showAlertDialog(mContext, android.R.drawable.ic_dialog_alert, getString(R.string.error_enter_invalid_pass), "Change password");
				
			} else {
				Utility.showWaitingDialog(progressDialog);
				DataHelper.getInstance().changePassword(EMarketing.getInstance().getAccountEmail(), pass1);
			}
		} else {
			Utility.showAlertDialog(mContext, android.R.drawable.ic_dialog_alert, getString(R.string.error_enter_math_pass), "Change password");
			
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		if(data == null) return;
		DataResult result = (DataResult) data;
		if(result.getMethodName() == Methods.changePass){
			Utility.hideWaitingStatus(progressDialog);			
			if(result.getErrorCode() == null){
				int responseCode = (Integer) result.getData();
				if(responseCode == 0) { 
					Utility.showAlertDialog(mContext, android.R.drawable.ic_dialog_alert, "Error", "Change password");
				} else if (responseCode == 1) {
					EMarketing.getInstance().setAccountPassword(changePassword);
					changePassSuccess();
				} 
			} else {
				Utility.showAlertDialog(mContext, android.R.drawable.ic_dialog_alert, "Error", "Change password");
			}
		}
	}	
	
	private void changePassSuccess(){
		EMarketing.getInstance().setExpiredLocal("");
		EMarketing.getInstance().setLogin(false);
		Intent intent = new Intent(ChangePassword.this, NewLogInActivity.class);			
		finish();
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
		startActivity(intent);
	}

}
